﻿using System.Collections.Generic;
using System.Linq;

namespace HEFramework
{
    public static class ElementSystem
    {
        [EntitySystem]
        public class ElementAwakeSystem : AwakeSystem<Element>
        {
            protected override void Awake(Element _self)
            {
                for (int i = (int)eElement.Gold; i < (int)eElement.Max; i++)
                {
                    _self.Elements.Add(i, 0);
                }
            }
        }

        public static void ChangeElement(this Element _self, int _id, int _lv = 1)
        {
            _self.Elements[_id] += _lv;
            if (_self.Elements[_id] <= 0)
            {
                _self.Elements[_id] = 0;
            }

            _self.UpdateElement(_id);
        }

        public static void UpdateElement(this Element _self, int _id)
        {
            var building = _self.GetSceneChild<Building>();
            var team = _self.GetSceneChild<Team>();


            switch (_id)
            {
                case (int)eElement.Gold:
                    if (building == null)
                    {
                        return;
                    }

                    _self.UpdateElementBuff(building, _id);
                    break;
                case (int)eElement.Wood:
                case (int)eElement.Water:
                case (int)eElement.Fire:
                case (int)eElement.Soil:
                    if (team == null)
                    {
                        return;
                    }

                    for (int i = 0; i < team.Heros.Count; i++)
                    {
                        _self.UpdateElementBuff(team.Heros[i], _id);
                    }

                    break;
            }
        }

        public static void UpdateHeroBuff(this Element _self, Entity _entity)
        {
            for (int i = (int)eElement.Gold + 1; i < (int)eElement.Max; i++)
            {
                _self.UpdateElementBuff(_entity, i);
            }
        }

        private static void UpdateElementBuff(this Element _self, Entity _entity, int _id)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            if (dc == null)
            {
                return;
            }

            int lv = _self.Elements[_id];
            var data = dc.LoadDataTable<DRElement>().GetData(_id);
            var lvs = data.Levels;
            var ids = data.SkillEffectIds;
            for (int i = 0; i < lvs.Length; i++)
            {
                if (i < ids.Length)
                {
                    if (lv >= lvs[i])
                    {
                        _self.UpdateElementBuff(_entity, ids[i], true);
                    }
                    else
                    {
                        _self.UpdateElementBuff(_entity, ids[i], false);
                    }
                }
            }
        }

        private static void UpdateElementBuff(this Element _self, Entity _entity, int _id, bool _add)
        {
            var sc = _entity.GetChild<SkillComponent>();
            if (sc == null)
            {
                return;
            }

            if (_add)
            {
                sc.ExecuteSkillEffect(_id);
            }
            else
            {
                sc.RemoveSkillEffect(_id);
            }
        }
    }
}